function TestValueObjGen() {
  this.testLodaFilterFromData = function() {
    filter = new Filter();
    filter.setData("code_id,detail_code_id,detail_code_name,detail_code_dsc,reg_dt,reg_id,avail_str_ymd,avail_end_ymd,use_yn");    
    
    AppsUnit.assert(filter.size() == 9);
  }
  
  this.testFixFuncCode = function() {
    filter = new Filter();
    AppsUnit.assert(filter.fixFuncCode("detail_code_dsc", "get") == "getDetailCodeDsc");
    AppsUnit.assert(filter.fixFuncCode("code", "get") == "getCode");
  }

  this.testFixFieldCode = function() {
    filter = new Filter();
    AppsUnit.assert(filter.fixFieldCode("detail_code_dsc") == "detailCodeDsc");
    AppsUnit.assert(filter.fixFieldCode("code") == "code");
  }
  
  
  this.testWriteHeader = function() {
    gen = new GenSource();
    gen.writeHeader("Code");
    AppsUnit.assert(gen.getCode() == "\nfunction Code() {\n")
  }  
  
  this.testWriteFields = function() {
    filter = new Filter();
    filter.setData("code_id,detail_code_id,detail_code_name,detail_code_dsc,reg_dt,reg_id,avail_str_ymd,avail_end_ymd,use_yn");    
    filter.loadFieldList();
    gen = new GenSource();
    gen.writeHeader("Code");
    gen.writeFieldSrc(filter.getFields());
    
    //Logger.log(gen.getCode());
    AppsUnit.assert(gen.getCodeLine(3) == "  this.detailCodeId;");
  }
  
  this.testWriteFuncs = function() {
    filter = new Filter();
    filter.setData("code_id,detail_code_id,detail_code_name,detail_code_dsc,reg_dt,reg_id,avail_str_ymd,avail_end_ymd,use_yn");    
    filter.loadFieldList();
    filter.loadFuncsList();
    gen = new GenSource();
    gen.writeFuncSrc(filter.getFields(), filter.getSetFuncList());
    gen.writeFuncSrc(filter.getFields(), filter.getGetFuncList());
    
    AppsUnit.assert(gen.getCodeLine(2) == "  this.setCodeId = function(codeId) {");
  }

  this.testWriteTail = function() {
    gen = new GenSource();
    gen.writeTail();
    AppsUnit.assert(gen.getCodeLine(1) == "function }")
  }

  this.testObjectForScriptDb = function() {
    gen = new GenSource();
    
  }  
  
}

TestValueObjGen.prototype = new AppsUnit.TestCase();

function GenSource() {
  this.src = "\nfunction ";
  
  this.writeHeader = function(cName) {
    this.src += cName+"() {\n";
  }
  
  this.writeFieldSrc = function(fields) {
    for(var i=0;i<fields.length;i++) {
      this.src += "  this."+fields[i]+";\n";
    }  
  }
  
  this.writeFuncSrc = function(fields, funcList) {
    this.src += "\n";
    for(var i=0;i<fields.length;i++) {
      this.src += "  this."+funcList[i]+" = function("+fields[i]+") {\n";
      this.src += "    this."+fields[i]+" = "+fields[i]+";\n";
      this.src += "  }\n\n";
    }  
  }
  
  this.writeTail = function() {
    this.src += "}";
  }

  
  this.getCode = function() {
    return this.src;
  }
  
  this.getCodeLine = function(num) {
    codeLines = this.src.split("\n");
    return codeLines[num];
  }  
}  

function Filter() {
  this.srcFields;
  this.fixFields   = new Array();
  this.setFuncList = new Array();
  this.getFuncList = new Array();
  
  this.setData = function(dataSrc) {
    this.srcFields = dataSrc.split(",");
  };
  
  this.loadFieldList = function() {
    for(var i=0;i<this.srcFields.length;i++) {
      this.fixFields.push(this.fixFieldCode(this.srcFields[i]));
    }
  }  
  
  this.loadFuncsList = function() {
    this.makeValueFuncs("set");
    this.makeValueFuncs("get");
  }  

  this.makeValueFuncs = function(prefix) {
    for(var i=0;i<this.srcFields.length;i++) {
      if(prefix == "set")
        this.setFuncList.push(this.fixFuncCode(this.srcFields[i], prefix));
      else 
        this.getFuncList.push(this.fixFuncCode(this.srcFields[i], prefix));
    }  
  }  

  this.fixFieldCode = function(element) {
    factors = element.split("_");
    
    result = factors[0];
    
    if(factors.length == 1) return result;
    
    
    for(var i=1;i<factors.length;i++) {
      result += factors[i].substring(0,1).toUpperCase()+factors[i].substring(1);
    }
    return result;
  };
  
  this.fixFuncCode = function(element, prefix) {
    factors = element.split("_");
    result = prefix;
    for(var i=0;i<factors.length;i++) {
      result += factors[i].substring(0,1).toUpperCase()+factors[i].substring(1);
    }
    return result;
  };

  this.getFields = function() {
    return this.fixFields;
  }; 

  this.getSetFuncList = function() {
    return this.setFuncList;
  }; 

  this.getGetFuncList = function() {
    return this.getFuncList;
  }; 
    
  this.size = function() {
    return this.srcFields.length;
  };  
}  

function command() {
  AppsUnit.runTest(new TestValueObjGen());
}  

function getCodeTest() {
  genCode("Code","code_id,detail_code_id,detail_code_name,detail_code_dsc,reg_dt,reg_id,avail_str_ymd,avail_end_ymd,use_yn");    
}  

function genCode(className, elements) {
    var filter = new Filter();
    filter.setData(elements);    
    filter.loadFieldList();
    filter.loadFuncsList(); 
    var gen = new GenSource();
    gen.writeHeader(className);
    gen.writeFieldSrc(filter.getFields());
    gen.writeFuncSrc(filter.getFields(), filter.getSetFuncList());
    gen.writeFuncSrc(filter.getFields(), filter.getGetFuncList());
    gen.writeTail();
    Logger.log(gen.getCode());   
}  